package com.xqt.base.dao;



import com.xqt.base.entity.BaseEntity;
import com.xqt.base.page.BasePage;
import com.xqt.base.page.PageInfo;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.List;
import java.util.Map;

public abstract class BaseDao<T> {

	@Autowired
	@Qualifier("readSqlSession")
	protected SqlSessionTemplate readSqlSession;

	@Autowired
	@Qualifier("writeSqlSession")
	protected SqlSessionTemplate writeSqlSession;


	public abstract String getReadSql();

	public abstract String getWriteSql();

	public T queryById(Object id){
		return readSqlSession.selectOne(getReadSql()+"queryById",id);
	}


	public int queryByCount(BaseEntity entity){
		return readSqlSession.selectOne(getReadSql()+"queryByCount",entity);
	}

	public void add(T object) {
		writeSqlSession.insert(getWriteSql()+"add",object);
	}

	public void updateById(T object) {
		writeSqlSession.update(getWriteSql()+"update",object);
	}

	public void updateBySelective(T object) {
		writeSqlSession.update(getWriteSql()+"updateBySelective",object);
	}

	public void delete(Object id){
		writeSqlSession.delete(getWriteSql()+"delete",id);
	}

	public List<T> queryByList(BaseEntity entity) {
		return   readSqlSession.selectList(getReadSql()+"queryByList",entity);
	}
	
	public List<T> queryPageList(BaseEntity entity) {
		entity.setLimitDirection(true);
		return   readSqlSession.selectList(getReadSql()+"queryByList",entity);
	}
	
	public PageInfo<T> queryPageInfo(BaseEntity entity){
		entity.setLimitDirection(true);
		int rowCount = queryByCount(entity);
		List<T> list = queryByList(entity);
		PageInfo<T> pageInfo = new PageInfo<T>(list, entity.getPageSize(), rowCount, entity.getPageId());
		return pageInfo;
	}
	
	/**
	 * 根据方法名称获取分页列表
	 * @param entity
	 * @param countMethod  获取数据总记录
	 * @param sqlMethod    获取数据list
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PageInfo<T> selectPageListCustom(T entity,String countMethod, String sqlMethod, int pageIndex, int pageSize){
		int recordCount = readSqlSession.selectOne(getReadSql()+countMethod.trim(),entity);
        int maxPage;
        maxPage=recordCount / pageSize+(recordCount%pageSize==0?0:1);
        if (pageIndex > maxPage) {
            pageIndex = maxPage;
        }
        int startIndex = 0;
        if (pageIndex != 0) {
            startIndex = (pageIndex - 1) * pageSize;
        }
        List<T> list = readSqlSession.selectList(getReadSql()+sqlMethod.trim(), entity,new RowBounds(startIndex, pageSize));
        PageInfo<T> pageInfo = new PageInfo<T>(list, pageSize, recordCount, pageIndex);
        return pageInfo;
	}
	
	/*
     * 获取分页列表【自定义分页】
     */
    public PageInfo<T> selectPageByEntity(BaseEntity entity, String countMethod, String sqlMethod){
        int recordCount = readSqlSession.selectOne(getReadSql()+countMethod.trim(),entity);
        List<T> list = readSqlSession.selectList(getReadSql()+sqlMethod.trim(), entity);
        PageInfo<T> pageInfo = new PageInfo<>(list, entity.getPageSize(), recordCount, entity.getPageId());
        return pageInfo;
    }
	
    
    public <E,F> List<E> selectEntityList(F entity,String sqlMethod){
    	return readSqlSession.selectList(getReadSql()+sqlMethod.trim(), entity);
    }
    
    
}
